package rauguelu.feria.tapas;

import java.io.IOException;
import java.util.List;

import rauguelu.items.Bar;
import rauguelu.lectorrss.MainActivity;
import rauguelu.programaEventos.OverlayMapa;
import rauguelu.programaEventos.R;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class Bares extends MapActivity{


	BaseDatosFeriasHelper miBBDDHelper;

	
	Typeface font;
	Typeface font2;
	
	@Override
	  public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        
	        setContentView(R.layout.mapa_bares);
	        
	        TextView tituloMapa = (TextView)findViewById(R.id.tituloMapa); 
	        
	        font = Typeface.createFromAsset(getAssets(), "VSVBPSB.TTF");
		  	font2 = Typeface.createFromAsset(getAssets(), "VSVAB.TTF");
		  	tituloMapa.setTypeface(font);
		  	
	        List<Bar> baresCursor = getBares();
	        
	        MapView mapa = (MapView)findViewById(R.id.mapa);
            mapa.setSatellite(false);	        
	        
		
	        mapa.setBuiltInZoomControls(true);

	      //A�adimos la capa de marcas
			/*List<Overlay> capas = mapa.getOverlays();
			OverlayMapa om = new OverlayMapa();
			capas.add(om);
			mapa.postInvalidate();*/
	        
	        //Controlador del mapa
	        
	       /* GeoPoint loc =
		            new GeoPoint(latitud.intValue(), longitud.intValue());
		 
		        controlMapa.setCenter(loc);*/
	        List<Overlay> mapOverlays = mapa.getOverlays();
	        Drawable drawable = this.getResources().getDrawable(R.drawable.restaurant);
	        CustomItemizedOverlay<CustomOverlayItem>  itemizedOverlay =  new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapa);
			
	        MapController mapController=null;
	        for(Bar bar: baresCursor)
	        {
	        	Double latitud = Double.parseDouble(bar.getLatitud())*1E6;
				Double longitud =  Double.parseDouble(bar.getLongitud())*1E6;
				GeoPoint point =  new GeoPoint(latitud.intValue(), longitud.intValue());
				CustomOverlayItem overlayitem = new CustomOverlayItem(point, bar.getNombre(),
						bar.getDescripcion(),this.getResources().getIdentifier(bar.getUrlImagen(),
								"drawable",  getApplicationContext().getPackageName()),bar.getId());
		 
				itemizedOverlay.addOverlay(overlayitem);
				mapOverlays.add(itemizedOverlay);
		 
				
				mapController = mapa.getController();
				mapController.setCenter(point);
				mapController.animateTo(point);
				mapController.setZoom(15);
	        }
	        GeoPoint pointCentro = new GeoPoint((int)3920755, (int)-215974);
	        mapController.setCenter(pointCentro);
		        
		        
	}
	
	public List<Bar> getBares()
	{
		 crearBBDDTapas();
	     
		 List<Bar> listaBares = null;
			
		miBBDDHelper.abrirBaseDatosFerias();
		listaBares = miBBDDHelper.fetchAllBares();
		miBBDDHelper.close();
		return listaBares;
	}
	  public void crearBBDDTapas() {
			miBBDDHelper = new BaseDatosFeriasHelper(this);
			try {
				miBBDDHelper.crearDataBaseFerias();
			} catch (IOException ioe) {
				throw new Error("Unable to create database");
			}
		}
	  
	  
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
